using.register("view", "/static/scripts/view.js");
using.register("controller", "/static/scripts/controller.js");

const animSpeed = 200;

$(document).ready(function () {
    using('view');
    using('controller');
    $('body').ajaxError(function () {
        alert('Operation failed because of an internal server error!')
    });

    questionEditor.hide();
    answerEditor.hide();
    ratingPnl.hide();

    questionToolbar.voteUpBtn.hide();
    questionToolbar.voteDownBtn.hide();
    questionToolbar.editBtn.hide();
    questionToolbar.openBtn.hide();

    answerToolbar.voteUpBtn.hide();
    answerToolbar.voteDownBtn.hide();
    answerToolbar.editBtn.hide();

    questionEditor.jqm({modal: true});
    ratingPnl.jqm({ajax: '/rating', modal: false});

    mainFrame.searchBtn.click(
        function () {
            loadQuestionsPage(1)
        });
    mainFrame.query.keypress(
        function (event) {
            if (event.which == 13) { //13 is the key code for the Enter
                event.preventDefault();
                loadQuestionsPage(1)
            }
        });
    mainFrame.askQuestionBtn.click(
        function () {
            questionEditor.jqmShow();
        });
    mainFrame.viewRatingBtn.click(
        function () {
            ratingPnl.jqmShow();
        });
    questionEditor.saveBtn.click(
        function () {
            postQuestion()
        });
    questionEditor.cancelBtn.click(
        function () {
            questionEditor.cleanup();
            questionEditor.jqmHide();
        });
    questionToolbar.voteUpBtn.click(
        function () {
           var last = $(this).getHolder();
           voteQuestionUp(last)
        });
    questionToolbar.voteDownBtn.click(
        function () {
           var last = $(this).getHolder();
           voteQuestionDown(last)
        });
    questionToolbar.editBtn.click(
        function () {
           var holder = $(this).getHolder();
            questionEditor.jqmShow();
            setTimeout(function () {
                questionEditor.edit(holder);
            }, animSpeed)
        });
    questionToolbar.openBtn.click(
        function () {
           var holder = $(this).getHolder();
           var cid = holder.getId();
           window.open('/question/' + cid, '_blank'); //opens a link in a separate tab or window
        });
    answerToolbar.voteUpBtn.click(
        function () {
           var answer = $(this).getHolder();
           voteAnswerUp(answer)
        });
    answerToolbar.voteDownBtn.click(
        function () {
           var answer = $(this).getHolder();
           voteAnswerDown(answer)
        });
    answerToolbar.editBtn.click(
        function () {
           var answer = $(this).getHolder();
           answerEditor.edit(answer);
        });
    answerEditor.saveBtn.click(
        function () {
            postAnswer()
        });
    answerEditor.cancelBtn.click(
        function () {
            if (!answerEditor.isEmpty()) {
                answerEditor.cleanup();
            } else {
                hideQuestion();
            }
        });
    answerEditor.on('mouseenter', '.list-item',
        function () {
            answerToolbar.attach(this);
        });
    answerEditor.on('mouseleave', '.list-item',
        function () {
            answerToolbar.detach();
        });
    questionToolbar.click(
        function (event) {
            event.stopPropagation();
        });
    questionsList.on('click', '.question-summary',
        function () {
            var prevItem = hideQuestion();
            var nextItem = $(this);
            //do nothing if the same question is clicked
            if (nextItem.get(0) === prevItem.get(0)) {
                return
            }
            showQuestion(nextItem);
        });
    loadQuestionsPage(1);
});

function showQuestion(question) {
    answerEditor.queue(function () {
        question.removeClass('unselected');
        question.addClass('selected');
        question.parent().append(answerEditor);
        answerEditor.show(animSpeed);
        question.parent().siblings().css('opacity', '0.7');
        loadAnswers(question.getId());
        //Find the heigh of the question's content in pixels
        //and expand the question to this height
        const height = getExcerptHeight(question) + 'px';
        question.parent().animate({height:height}, animSpeed, function () {
            questionToolbar.attach(question);
            answerEditor.textarea.focus();
        });
        $(this).dequeue();
    });
}

function hideQuestion() {
    var question = questionsList.getSelected();
    if (question.size() != 0) {
        question.parent().animate({height:qtabHeight + 'px'}, animSpeed);
        question.removeClass('selected');
        question.addClass('unselected');
        questionToolbar.detach();
        question.parent().siblings().css('opacity', '1.0');
        answerEditor.hide(animSpeed, function () {
            commentsList.html('');
            answerEditor.cleanup();
            dock.append(answerEditor);
        });
    }
    return question;
}

function getExcerptHeight(question) {
    const height = question.find('.excerpt').height();
    return Math.max(qtabHeight, height + 24);
}

